package fun.sssdnsy.util;

import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonUtil {

    private static final ObjectMapper mapper = new ObjectMapper();

    public static <T> T fromJson(String json, Class<T> type) {

        try {

            return mapper.readValue(json, type);

        } catch (Exception e) {
            throw new RuntimeException("JSON反序列化失败", e);
        }

    }

    public static String toJson(Object obj) {

        try {

            return mapper.writeValueAsString(obj);

        } catch (Exception e) {
            throw new RuntimeException("JSON序列化失败", e);
        }

    }

}
